import { computed, onMounted, ref } from 'vue'
import { getDepts } from '@/api/system/dept'
import { queryRoleRelevanceUsers, queryAvailableYears } from '@/api/modelQuality/inspectPlan'
import { get } from '@/api/system/dictDetail'
export function useGetConfig() {
  // 表格配置项
  const tableColumn = ref([
    {
      type: 'selection',
      dataIndex: 'selection'
    },
    {
      title: '评审场次',
      dataIndex: 'reviewMonth',
      option: 'inspect_plan_review_month'
    },
    {
      title: '主承接',
      dataIndex: 'deptName'
    },
    {
      title: '工程号/主合同号/项目编号',
      width: 240,
      dataIndex: '--',
      render: (e) => `
    ${e.constructionCode || '空'} /
    ${e.contractCode || '空'} /
    ${e.projCode || '空'}`
    },
    {
      title: '项目名称',
      width: 240,
      dataIndex: 'projName'
    },
    {
      title: '子项号/子项名称',
      width: 160,
      dataIndex: 'subNo',
      render: (e) => `${e.subNo} / ${e.subName}`
    },
    {
      title: '申报节点',
      dataIndex: 'declareNode',
      option: 'inspect_plan_declare_node'
    },
    {
      title: '检查方',
      width: 160,
      dataIndex: '---',
      render: (e) => `${e.inspect1CompanyName} / ${e.inspect2CompanyName}`
    },
    {
      title: '观察员',
      dataIndex: 'observerName'
    },
    {
      title: '会议日期',
      dataIndex: 'meetingDate',
      width: 100
    },
    {
      title: '会议时间',
      width: 160,
      dataIndex: 'meetingTime'
    },
    {
      title: '更新者',
      dataIndex: 'updateBy'
    },
    {
      title: '更新时间',
      dataIndex: 'updateTime',
      width: 160
    },
    {
      title: '审查状态',
      width: 120,
      dataIndex: '_shenchaStatus',
      slot: 'table_column_status',
      fixed: 'right'
    },
    {
      title: '操作',
      width: 200,
      dataIndex: 'operate',
      slot: 'table_operate',
      align: 'center',
      fixed: 'right'
    }
  ])

  // 搜索配置项
  const searchColumn = ref([
    {
      type: 'input',
      width: 300,
      dataIndex: 'searchColumn',
      label: '工程号、主合同号、项目编号及项目名称'
    },
    {
      type: 'select',
      dataIndex: 'reviewYear',
      label: '年份',
      clearable: true,
      option: computed(() => years.value)
    },
    {
      type: 'select',
      dataIndex: 'reviewMonth',
      label: '评审场次',
      option: 'inspect_plan_review_month'
    },
    {
      type: 'select',
      dataIndex: 'deptId',
      label: '主承接',
      optionValue: 'id',
      option: computed(() => mainUndertaking.value)
    },
    {
      type: 'select',
      dataIndex: 'declareNode',
      label: '申报节点',
      option: 'inspect_plan_declare_node'
    },
    {
      type: 'select',
      dataIndex: 'inspectCompanyId',
      label: '检查方',
      optionValue: 'id',
      option: computed(() => mainUndertaking.value)
    },
    {
      type: 'select',
      dataIndex: 'observerId',
      label: '观察员',
      option: computed(() => observer.value),
      optionLabel: 'label',
      optionValue: 'userId'
    },
    {
      type: 'select',
      dataIndex: 'inspectStatus',
      label: '审查状态',
      option: computed(() => inspectPlanStatus.value)
    }
  ])

  // 默认表单数据
  const defaultForm = {
    reviewYear: new Date().getFullYear().toString(),
    finishPlanTime: null,
    startInspectTime: null,
    reviewMonth: null,
    projId: null,
    subId: null,
    declareNode: null,
    designSoftware: null,
    designSoftware_: null,
    remark: null,
    inspect1CompanyId: null,
    inspect2CompanyId: null,
    observerId: null,
    meetingDate: null,
    meetingTime: null,
    presideCompanyId: null,
    meetingNo: null,
    meetingPassword: null,
    meetingRemark: null
  }

  // 表单效验
  const formRules = {
    reviewYear: [
      { required: true, message: '评审年份不能为空', trigger: 'blur' }
    ],
    reviewMonth: [
      { required: true, message: '评审场次不能为空', trigger: 'blur' }
    ],
    projId: [
      {
        required: true,
        message: '工程号/主合同号/项目编号不能为空',
        trigger: 'blur'
      }
    ],
    subId: [{ required: true, message: '子项号不能为空', trigger: 'blur' }],
    inspect1CompanyId: [
      { required: true, message: '检查方1公司不能为空', trigger: 'blur' }
    ],
    inspect2CompanyId: [
      { required: true, message: '检查方2公司不能为空', trigger: 'blur' }
    ],
    declareNode: [
      { required: true, message: '申报节点不能为空', trigger: 'blur' }
    ]
  }

  // 权限
  const permission = {
    add: ['admin', 'inspectPlan:add'],
    edit: ['admin', 'inspectPlan:edit'],
    del: ['admin', 'inspectPlan:del'],
    turnover: ['admin', 'inspectPlan:turnover'],
    download: ['admin', 'inspectPlan:download'],
    scoreResult: ['admin', 'inspectPlan:scoreResult'],
    analyse: ['admin', 'inspectPlan:analyse']
  }

  // 获取 主承接 下拉框的枚举字段
  const mainUndertaking = ref()
  const getMainUndertaking = async() => {
    const params = { enabled: true, isExternal: false }
    const _res = await getDepts(params)
    const pid = _res.content[0].id
    const res = await getDepts({ ...params, pid })
    const arr = res.content.filter((item) => item.label !== '股份总部')
    mainUndertaking.value = arr
  }

  // 获取 年份值
  const years = ref([])
  const getYears = async() => {
    const res = await queryAvailableYears()
    years.value = Array.isArray(res) ? res.map(item => ({
      label: item,
      value: item
    })) : []
  }

  // 获取 观察员 下拉框的枚举字段
  const observer = ref()
  const getObserver = () => {
    queryRoleRelevanceUsers({ roleName: '观察员' }).then((res) => {
      observer.value = res.map(item => {
        item.label = `${item.nickName} （${item.username}）`
        return item
      })
    })
  }
  // 获取审查状态
  const inspectPlanStatus = ref()
  const getStatus = () => {
    get('inspect_plan_status').then(res => {
      inspectPlanStatus.value = res.content
    })
  }

  const totalScoreMap = {
    proj: '项目策划',
    design: '设计质量',
    required: '模型质量-必选项部分',
    noRequired: '模型质量-可选项部分'
  }

  onMounted(() => {
    getMainUndertaking()
    getObserver()
    getStatus()
    getYears()
  })

  return {
    tableColumn,
    searchColumn,
    defaultForm,
    formRules,
    permission,
    mainUndertaking,
    years,
    observer,
    inspectPlanStatus,
    totalScoreMap

  }
}
